﻿using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WCS.Entity;
using WCS.Entity.SYS;
using WCS.Helpers;
using WCS.Helpers.ExportExcel;
using WCS.Utils;

namespace WCS.Controls
{
    public partial class PLCErrorRec : UIPage
    {
        public PLCErrorRec()
        {
            InitializeComponent();
        }

        public static string connStrings = System.Configuration.ConfigurationManager.AppSettings["SQLConn"].ToString();
        /// <summary>
        /// 数据库对象
        /// </summary>
        static SqlSugarDapper _SqlSugarDapper = SqlSugarDapper.GetDbContext();

        Dictionary<string, string> _MaterialType = new Dictionary<string, string>();
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SortingTask_Load(object sender, EventArgs e)
        {

            TaskTableBind();
            this.uiPagination1.ActivePage = 1;
            this.uiPagination1.TotalCount = 10000;
            dgvScoreList.RowHeadersVisible = false;
            uiComboBox1.Items.Add("in");
            uiComboBox1.Items.Add("out");

        }
     
        /// <summary>
        /// 成绩绑定
        /// </summary>
        private void TaskTableBind()
        {
            //获取当前页面的index
            int index = this.uiPagination1.ActivePage - 1;
            int pageIndex = this.uiPagination1.ActivePage;
            int size = this.uiPagination1.PageSize;
            int begin = (index * size) + 1;
            int end = (index + 1) * size;
            int count = 0;
            //int cMawbNo = (int)this.TaskState.SelectedIndex;
            //this.dgvScoreList.DataSource = "select seqId,createTime,importNo,uldNo,flightNo,etd,mawbNo,pieces,scanPieces,sortedPieces,lightPieces,stackPieces,totalPieces,receiveTime from UldInfo".QueryStrCount(out count);

            //var asd = TaskState.SelectedValue.ToString();
            this.dgvScoreList.DataSource = _SqlSugarDapper.DB.Queryable<PLCErrorRecord>()
                .WhereIF(!uiComboBox1.Text.IsNullOrEmpty(), w => w.PlcErrorName == uiComboBox1.Text)
                .OrderBy("PlcErrorTime desc")//然后根据创建时间排序
                .ToPageList(pageIndex, size, ref count);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFind_Click(object sender, EventArgs e)
        {
            TaskTableBind();
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            TaskTableBind();
        }

        /// <summary>
        /// 页面改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pagingSource"></param>
        /// <param name="pageIndex"></param>
        /// <param name="count"></param>
        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            TaskTableBind();
        }

        private void uiComboBox1_DropDown(object sender, EventArgs e)
        {
            var list = _SqlSugarDapper.DB.Queryable<PlcError>()
                .ToList()
                .OrderBy(w => w.PlcErrorName)
                .GroupBy(w => w.PlcErrorName)
                .ToList();

            uiComboBox1.Items.Clear();
            foreach (var item in list)
            {
                uiComboBox1.Items.Add(item.Key);
            }
        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            var Starttime = starttime.Text.Trim();
            var Endtime = endtime.Text.Trim();

            if (uiComboBox1.Text == "")
            {
                var dataTable = _SqlSugarDapper.DB.Ado.GetDataTable($"Select PlcErrorName as '报错信息',PlcErrorTime as '报错时间' from PLCErrorRecord where PlcErrorTime between '{Starttime}' and '{Endtime}'");

                if (dataTable != null)
                {
                    string path = "";
                    SaveFileDialog saveDialog = new SaveFileDialog();
                    saveDialog.Title = "请选择要导出的位置";
                    saveDialog.Filter = "Excel文件| *.xlsx;*.xls";
                    saveDialog.ShowDialog();
                    path = saveDialog.FileName;
                    if (path.IndexOf(":") < 0) return; //判断是否点击取消

                    ExportToExcel._exportToExcel.DataSetToExcel1(dataTable, path, true);
                }
            }
            else
            {
                var dataTable = _SqlSugarDapper.DB.Ado.GetDataTable($"Select PlcErrorName as '报错信息',PlcErrorTime as '报错时间' from PLCErrorRecord where PlcErrorTime between '{Starttime}' and '{Endtime}' AND PlcErrorName='{uiComboBox1.Text}'");

                if (dataTable != null)
                {
                    string path = "";
                    SaveFileDialog saveDialog = new SaveFileDialog();
                    saveDialog.Title = "请选择要导出的位置";
                    saveDialog.Filter = "Excel文件| *.xlsx;*.xls";
                    saveDialog.ShowDialog();
                    path = saveDialog.FileName;
                    if (path.IndexOf(":") < 0) return; //判断是否点击取消

                    ExportToExcel._exportToExcel.DataSetToExcel1(dataTable, path, true);
                }
            }
        }
    }
}
